//
//  PurchaseAllListViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/1.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "PurchaseAllListViewModel.h"
#import "PurchaseListTableViewHeaderFooterView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "HomeHotPictureTableViewCell.h"
#import "HomeHotOrderTableViewCell.h"
#import "PurchaseAllListModel.h"
#import "GLGWPopTosView.h"
#import "OrderOperationView.h"

@interface PurchaseAllListViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *selfTableView;
}
@property (nonatomic, strong) PurchaseAllListModel *model;

@end

@implementation PurchaseAllListViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(44);
        tableView.delegate = self;
        tableView.dataSource = self;
        [tableView registerClass:[PurchaseListTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"PurchaseListTableViewHeaderFooterView"];
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [superView addSubview:tableView];
        selfTableView = tableView;
        [self requestPurchaseList];
    }
    return self;
}

#pragma mark --- UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.model) {
        return self.model.data.infos.count;
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableViewCell *cell;
    if (self.model) {
        PurchaseAllListDataInfosModel *infoData = self.model.data.infos[indexPath.section];
        if (infoData.status == 3) {  // 图片求购
            HomeHotPictureTableViewCell *cell_picture = [HomeHotPictureTableViewCell cellWithTableView:tableView];
            cell_picture.purchaseListModel = infoData;
            cell = cell_picture;
        } else {
            HomeHotOrderTableViewCell *cell_hot = [HomeHotOrderTableViewCell cellWithTableView:tableView];
            cell_hot.purchaseListModel = infoData;
            cell = cell_hot;
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    PurchaseListTableViewHeaderFooterView *footView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PurchaseListTableViewHeaderFooterView"];
    footView.type = self.type;
    footView.section = section;
    if (self.model) {
        PurchaseAllListDataInfosModel *infoData = self.model.data.infos[section];
        footView.dataInfoModel = infoData;
        kWeakSelf(self);
        footView.BlockBottomBtn = ^(GLGWButton * _Nonnull btn, NSInteger section) {
            [weakself bottomBtnClikWith:btn section:section];
        };
    }
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(40);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(purchaseDidSelectRowAtModel:)]) {
        PurchaseAllListDataInfosModel *infoData = self.model.data.infos[indexPath.section];
        [self.delegate purchaseDidSelectRowAtModel:infoData];
    }
}
/*
 0: 刷新求购单
 1: 删除
 2: 查看退款
 3: 查看订单
 */
- (void)bottomBtnClikWith:(GLGWButton *)btn section:(NSInteger)section
{
    switch (btn.tag) {
        case 320:{ // 刷新求购单
            GLGWPopTosView *popV = [[GLGWPopTosView alloc] initWithPopView:PopViewType_Success];
            popV.tosTitle = @"刷新成功";
        }
            break;
        case 321: // 删除
            [self showAlertV:@"您正在删除求购单，确认删除？" strArr:@[@"我再看看", @"确认删除"]];
            break;
        case 322: // 查看退款
            break;
        case 323: // 查看订单
            break;
        default:
            break;
    }
    PurchaseAllListDataInfosModel *infoData = self.model.data.infos[section];
    if (self.BlockBottomBtn) {
        self.BlockBottomBtn(btn, infoData);
    }
}

- (void)showAlertV:(NSString *)str strArr:(NSArray *)arr
{
    OrderOperationView *opeartionView = [[OrderOperationView alloc] initWithPopOrderOperation:arr theTitleStr:@"操作提示" theTosStr:str];
    [opeartionView show];
    opeartionView.Block_CaseAction = ^(NSInteger tag) {
        if (tag == 311) {
//            [self.delegate payTheOrder];
        }
    };
}

#pragma mark - 请求接口
// 求购列表
- (void)requestPurchaseList
{
    @weakify(self)
    _service_RecommendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        RACSignal * requestSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSMutableDictionary * parDic = (NSMutableDictionary *)input;
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyOrderBuyerGoods] lodingView:nil lodingTextString:@"" isLoding:YES parameters:parDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return [requestSignal map:^id _Nullable(id  _Nullable value) {
            self.model = [PurchaseAllListModel yy_modelWithJSON:value];
            if (self.model.code == 0) {
                [self->selfTableView reloadData];
            }
            return self.model;
        }];
    }];
}

@end
